﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
using SMBCFamilyAssessment.Models;
using System.Web.Services;
using Telerik.Web.UI;
using System.Text.RegularExpressions;

namespace SMBCFamilyAssessment.Models.Persons
{
    public partial class Insert : System.Web.UI.Page
    {
		protected SMBCFamilyAssessment.Models.AssessmentContext _db = new SMBCFamilyAssessment.Models.AssessmentContext();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        // This is the Insert method to insert the entered Person item
        // USAGE: <asp:FormView InsertMethod="InsertItem">
        public void InsertItem()
        {
            using (_db)
            {
                var item = new SMBCFamilyAssessment.Models.Person();

                TryUpdateModel(item);

                if (ModelState.IsValid)
                {
                    // Save changes

                    // Save changes

                    //////

                   
                      TextBox t = (TextBox)FormView1.FindControl("TextBoxPropertyId");
                        if (t != null)
                            if (!string.IsNullOrEmpty(t.Text))
                                item.FamilyId = Convert.ToInt32(t.Text);
                    

                    ////////


                    _db.Persons.Add(item);
                    _db.SaveChanges();

                    Response.Redirect("Default");
                }
            }
        }

        protected void ItemCommand(object sender, FormViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Cancel", StringComparison.OrdinalIgnoreCase))
            {
                Response.Redirect("Default");
            }
        }

        protected void RadSearchBox1_Search(object sender, SearchBoxEventArgs e)
        {
            var regexItem = new Regex("^[a-zA-Z0-9 ]*$");
            if (e != null)
            {
                if (!string.IsNullOrEmpty(e.Text) )
                {
                      TextBox t = (TextBox)FormView1.FindControl("TextBoxPropertyId");
                    if (regexItem.IsMatch(e.Text))
                    {

                        Label1.Text = "Text : " + e.Text + e.Value;

                        //TextBox t = (TextBox)FormView1.FindControl("TextBoxPropertyId");
                        t.Text = e.Value.ToString();
                    }
                    else
                    {
                        Label1.Text = "Invalid search string " + e.Text + e.Value;
                        t.Text = "";
                    }
                }
            }
        }

        [WebMethod]
        public static SearchBoxData GetData(SearchBoxContext context)
        {

            List<SearchBoxItemData> result = new List<SearchBoxItemData>();
            // DataTable data= SMBCFamilyAssessment.Logic.AssessmentBLcs.GetPropertyAddressData(context.Text);
            List<SMBCFamilyAssessment.Models.Family> list = SMBCFamilyAssessment.Logic.AssessmentBLcs.GetPropertyAddressData(context.Text);
            for (int i = 0; i < list.Count; i++)

            //  foreach(SMBCFamilyAssessment.Models.Propertyaddress p in list)
            {
                SearchBoxItemData itemData = new SearchBoxItemData();
                //itemData.Text = data.Rows[i]["PropertyaddressId"].ToString();
                //itemData.Value = data.Rows[i]["Address1"].ToString();
                itemData.Text = list[i].Address1 + "  " + list[i].Address2 + "  " + list[i].PostCode;
                itemData.Value = list[i].FamilyId.ToString();
                result.Add(itemData);
            }

            SearchBoxData res = new SearchBoxData();
            res.Items = result.ToArray();

            return res;


        }






    }
}
